﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using DataAccess.Models;
using DataAccess.Repository;

namespace KPI.Controls
{
	public partial class GroupNav : System.Web.UI.UserControl
	{
		private IGroupRepository _GroupRepository;
		private List<Group> _listGroups;
		private Group _CurrentGroup;

		public int CurrentGroupID
		{
			get
			{
				return CommonHelper.QueryStringInt("groupid");
			}
		}

		public Group CurrentGroup
		{
			get
			{
				if(_CurrentGroup == null)
				{
					if(ListGroups != null && ListGroups.Count > 0 && CurrentGroupID > 0)
					{
						_CurrentGroup = ListGroups.FirstOrDefault(g => g.GroupID == CurrentGroupID);
					}
				}

				return _CurrentGroup;
			}
		}

		public List<Group> ListGroups
		{
			get
			{
				if(_listGroups == null)
				{
					if(Cache["allgroups"] == null)
					{
						Cache.Add("allgroups", GetGroups(), null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(4, 0, 0), System.Web.Caching.CacheItemPriority.Default, null);
					}

					_listGroups = Cache["allgroups"] as List<Group>;
				}

				return _listGroups;
			}
		}

		public string NavPage
		{
			get;
			set;
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				BindGroupCategory();
			}
		}

		protected void rptGroupCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			var data = e.Item.DataItem as GroupCategory;

			if(data != null)
			{
				var lblGroupCategory = e.Item.FindControl("lblGroupCategory") as Label;
				var rptGroup = e.Item.FindControl("rptGroup") as Repeater;
				var groupCategory = e.Item.FindControl("groupCategory") as System.Web.UI.HtmlControls.HtmlGenericControl;
				var groupsInCategory = e.Item.FindControl("groupsInCategory") as System.Web.UI.HtmlControls.HtmlGenericControl;

				lblGroupCategory.Text = data.GroupCategoryName;

				rptGroup.ItemDataBound += new RepeaterItemEventHandler(rptGroup_ItemDataBound);

				rptGroup.DataSource = GetGroupByCategory(data.GroupCategoryID);
				rptGroup.DataBind();

				if(CurrentGroup != null && CurrentGroup.GroupCategoryID != data.GroupCategoryID)
				{
					groupCategory.Attributes.Add("class", "groupcategory");
					groupsInCategory.Style.Add("display", "none");
				}
				else
				{
					groupCategory.Attributes.Add("class", "groupcategory collapsedgroup");
				}
			}
		}

		protected void rptGroup_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			var data = e.Item.DataItem as Group;

			if(data != null)
			{
				var hlGroup = e.Item.FindControl("hlGroup") as HyperLink;

				hlGroup.Text = data.GroupDispName;

				if(NavPage.Contains("?"))
				{
					hlGroup.NavigateUrl = NavPage + "&groupid=" + data.GroupID.ToString();
				}
				else
				{
					hlGroup.NavigateUrl = NavPage + "?groupid=" + data.GroupID.ToString();
				}

				if(data.GroupID == CurrentGroupID)
				{
					hlGroup.Enabled = false;
					hlGroup.CssClass = "disable";
				}
			}
		}

		protected void Page_UnLoad(object sender, EventArgs e)
		{
			if(_GroupRepository != null)
			{
				((IDisposable)_GroupRepository).Dispose();
			}
		}

		private List<Group> GetGroupByCategory(int categoryID)
		{
			List<Group> result = null;

			if(categoryID > 0 && ListGroups != null && ListGroups.Count > 0)
			{
				var groups = ListGroups.Where(g => g.GroupCategoryID == categoryID);

				if(groups != null)
				{
					result = groups.ToList();
				}
			}

			return result;
		}

		private void BindGroupCategory()
		{
			if(ListGroups != null && ListGroups.Count > 0)
			{
				List<GroupCategory> lstGroupCategory = new List<GroupCategory>();

				foreach(var item in ListGroups)
				{
					if(item.TheGroupCategory != null && lstGroupCategory.Count(gc => gc.GroupCategoryID == item.TheGroupCategory.GroupCategoryID) == 0)
					{
						lstGroupCategory.Add(item.TheGroupCategory);
					}
				}

				rptGroupCategory.DataSource = lstGroupCategory;
				rptGroupCategory.DataBind();
			}
		}

		private List<Group> GetGroups()
		{
			if(_GroupRepository == null)
			{
				_GroupRepository = new GroupRepository();
			}

			return _GroupRepository.GetAllGroups();
		}
	}
}